<?php
/**
 * Хендлер для получения всех сотрудников компании
 **/ 
class SetUserOrganization extends WallrideHandler{

  protected
    $action = false,
    $organizationId = false,
    $userId = false;

	public function setParams($params){  
    if (!empty($params['action'])) $this->action = $params['action'];
    if (!empty($params['userId']) && preg_match('/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/', $params['userId']))
        $this->userId = $params['userId'];
    if (!empty($params['organizationId']) && preg_match('/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/', $params['organizationId']))
        $this->organizationId = $params['organizationId'];
    
	}

	public function process(){
    if ($this->action != 'set_user_organization') return false;
    if (!$this->userId) return false;

    // получаем ассоциированный массив подразделений
    require_once 'models/user/user.php';
    $user = UserModel::getByUserID($this->userId);
    if (!$user){
      Logger::error("Cannot get user by id {$this->userId} in SetUserOrganization");
      $this->writeDebug("Cannot get user by id {$this->userId}");
      return false;
    }
    $user->data['organization_id'] = ($this->organizationId)?$this->organizationId:null;
    $user->save();
    $this->writeDebug("OK!");
    return true;
	}

}
?>
